/*
 * Created on 2005-7-6
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
package com.sohu.sip.auth.util;

import com.novell.ldap.LDAPConnection;
import com.sohu.peak.util.Logger;
import com.sohu.sip.auth.constant.AuthConstant;

/**
 * @author Dingyongxing
 * SOHU-R&D
 * 
 */
public class LDAPVerify {
	private static int ldapVersion = LDAPConnection.LDAP_V3;
	private static Logger log = new Logger();

	public static boolean verfifyUser(String username, String password) {
		LDAPConnection lc = new LDAPConnection();
		//sundongzhi
//		String ldapHost = CacheUtil.getConfigParamter("ldapHost");//10.1.34.106
//		String _ldapPort = CacheUtil.getConfigParamter("ldapPort");//389
		String ldapHost = AuthConstant.ldapHost;//10.1.34.106
		String _ldapPort = AuthConstant.ldapPort;//389
		log.debug("AuthConstant.ldapHost:%s",AuthConstant.ldapHost);
		log.info("ldapHost:%s",ldapHost);
		if (username == null)
			return false;
		try {
			// connect to the server
			int ldapPort = 389;
			try {
				ldapPort = Integer.parseInt(_ldapPort);
			} catch (Exception e) {
			}
			lc.connect(ldapHost, ldapPort);

			// authenticate to the server
			lc.bind(ldapVersion, username + "@sohu-inc.com", password
					.getBytes("UTF8"));
			boolean verifyResult = lc.isBound();
			lc.disconnect();
			return verifyResult;
		} catch (Exception e) {
			log.error("%s/%s login fail", username, password);
			return false;
		}
	}
}